<template>
  <div class="app-container">
    <div style="width: 100%; margin-bottom: 10px">
      <el-button type="primary" size="mini" @click="submitApprove" :disabled="approveStatus === '通过后不可再次提交审批'">{{
        approveStatus }}</el-button>
<!--      <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
    </div>
    <el-table v-loading="loading" border :data="detailList" style="width: 100%">
      <el-table-column label="编号" width="50" type="index" align="center" fixed="left" :index="index => index + 1" />
      <el-table-column prop="score" label="图片" align="center" :width="imageColWidth">
        <template v-slot="{row}">
          <template v-if="!row.editFlag">
            <image-preview  v-for="(item,index) in row.imageArr"
                       style="width: 100px; height: 100px; margin-right: 15px"
                       :src="item">
            </image-preview>
          </template>
          <!--          <span  v-if="!row.editFlag">{{row.images }}</span>-->
          <ImageUpload  v-if="row.editFlag" v-model="row.images" />
        </template>
      </el-table-column>
      <el-table-column prop="examiner" label="质检人" align="center" width="300">
        <template v-slot="{row}">
          <span  v-if="!row.editFlag">{{ row.examiner }}</span>
          <el-select v-if="row.editFlag" v-model="row.examinerArr" placeholder="质检人" clearable :style="{width: '100%'}" multiple>
            <el-option v-for="(item, index) in dict.type.examine_quality_inspection" :key="index" :label="item.label"
                       :value="item.value" ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="times" label="考核记录" align="center" width="250" cell-style="cellStyle">
        <template #default="scope">
          <span v-if="!scope.row.editFlag">{{ scope.row.times }}</span>
          <el-input type="textarea" v-if="scope.row.editFlag" v-model="scope.row.times" :rows="4">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="score" label="扣分" align="center" width="120">
        <template #default="scope">
          <span style="color: red" v-if="!scope.row.editFlag">{{ scope.row.score }}</span>
          <el-input v-if="scope.row.editFlag" v-model="scope.row.score">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="big" label="考核大项" align="left" width="180">
        <template #default="scope">
          <p style="white-space: pre-wrap;" v-show="true">{{ scope.row.big }}</p>
<!--          <el-input v-show="scope.row.editFlag" v-model="scope.row.big">-->
<!--          </el-input>-->
        </template>
      </el-table-column>
      <el-table-column prop="small" label="考核小项" align="left" width="300">
        <template #default="scope">
          <p style="white-space: pre-wrap;" v-show="true">{{ scope.row.small }}</p>
<!--          <el-input type="textarea" v-show="scope.row.editFlag" v-model="scope.row.small" :rows="4">-->
<!--          </el-input>-->
        </template>
      </el-table-column>
      <el-table-column prop="standard" label="考核标准" align="left" width="120">
        <template #default="scope">
          <p style="white-space: pre-wrap;" v-show="true">{{ scope.row.standard }}</p>
<!--          <el-input type="textarea" v-show="scope.row.editFlag" v-model="scope.row.standard">-->
<!--          </el-input>-->
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" width="100" align="center" />
      <el-table-column label="更新人" prop="updateBy" width="100" align="center" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" width="140">
        <template #default="scope">
          <div style="">
            <el-button size="small" type="text" icon="CircleCheck" v-show="scope.row.editFlag"
              :disabled="approveStatus === '通过后不可再次提交审批'" @click="submit(scope.row, $index)">保存
            </el-button>
            <el-button size="small" type="text" icon="CircleClose" v-show="scope.row.editFlag"
              :disabled="approveStatus === '通过后不可再次提交审批'" @click="cancel(scope.row, $index)">取消
            </el-button>
            <el-button size="small" icon="Edit" type="text" v-show="!scope.row.editFlag"
              :disabled="approveStatus === '通过后不可再次提交审批'" @click="edit(scope.row, $indx)"> 编辑
            </el-button>
<!--            <el-button size="small" icon="Delete" type="text" v-show="!scope.row.editFlag"-->
<!--              :disabled="approveStatus === '通过后不可再次提交审批'" @click="del(scope.row, $index)">删除-->
<!--            </el-button>-->
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>


<script>
import { add, list, listAll, get, update, del } from "@/api/social/examine/recordDetail";
import { getByActivityId, addApproveMission } from "@/api/approve/mission";
import item from "@/layout/components/Sidebar/Item.vue";

export default {
  name: "PlanDetail",
  dicts: ['examine_quality_inspection'],
  data() {
    return {
      imageColWidth:160,
      baseUrl: process.env.VUE_APP_BASE_API,
      id: undefined,
      // 遮罩层
      loading: true,
      approveStatus: "提交审批",
      // 菜品明细表格数据
      detailList: [],
      garbageTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
    };
  },
  created() {
    this.id = this.$route.params && this.$route.params.id;
    this.getList();
  },
  methods: {
    //处理图片显示
    handleImg(item){
      let imageArr=[]
      if (item.images ) {
        imageArr = item.images.split(",");
      }
      return imageArr
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAll({
        recordId: this.id,
      }).then(response => {
          let maxWith = 1;
        response.rows.map(item=>{
          if(item.examiner){
            item.examinerArr=item.examiner.split(",")
          }
          item.imageArr=this.handleImg(item)
          if (item.imageArr.length > maxWith) {
            maxWith = item.imageArr.length;
          }
        })
          this.imageColWidth = maxWith * 160;
        this.detailList = response.rows;
        this.loading = false;
      }
      );
      getByActivityId({
        activityId: this.id,
        activityType: "考核记录审批"
      }).then(res => {
        if (res.data == null) {
          return;
        }
        this.approveId = res.data.id;
        if (res.data.status === "拒绝") {
          this.approveStatus = "再次提交审批";
        } else if (res.data.status === "通过") {
          this.approveStatus = "通过后不可再次提交审批";
          this.addButtonStatus = "通过后不可添加或更改";
        } else {
          this.approveStatus = "查看审批";
        }
      })
    },
    addCommand: function () {
      var detail = {
        recordId: this.id
      }
      add(detail).then(res => {
        this.getList();
      })
    },
    submitApprove: function () {
      if (this.approveStatus === "查看审批") {
        this.$router.push("/approve/detail/" + this.approveId);
        return;
      }
      addApproveMission({
        activityType: "考核记录审批",
        activityId: this.id,
        name: "考核记录审批",
        status: "审批中"
      }).then(res => {
        this.$modal.msgSuccess("提交成功");
        this.approveStatus = "查看审批";
        this.approveId = res.data.id;
      });
    },
    submit: function (row, index) {
      let updateData = {
        id: row.id,
        big: row.big,
        small: row.small,
        standard: row.standard,
        score: row.score,
        times: row.times,
        images: row.images,
        examiner:row.examinerArr.toString(),
      }
      update(updateData).then(res => {
        this.$modal.msgSuccess("更新成功");
        row.editFlag = false;
        this.getList();
      })
    },
    cancel: function (row, index) {
      row.editFlag = false;
      this.getList();
    },
    edit: function (row, index) {
      row.editFlag = true;
      this.$set(this.detailList, index, row);
    },
    del: function (row, index) {
      del(row.id).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      });
    }
  }
};
</script>
<style scoped>
.el-input__inner {
  height: 100px;
}
::v-deep .el-upload-list__item{
  width: 80px;
  height: 80px;
}
::v-deep .el-upload--picture-card{
  width: 80px;
  height: 80px;
  line-height: 90px;
}
</style>
